const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function generateOrderId() {
  var outTradeNo = ""; //订单号
  for (var i = 0; i < 6; i++) //6位随机数，用以加在时间戳后面。
  {
    outTradeNo += Math.floor(Math.random() * 10);
  }
  outTradeNo = new Date().getTime() + outTradeNo; //时间戳，用来生成订单号。
  return outTradeNo;
}
function arrayInclude(arr,item){
  for(var i =0;i< arr.length;i++){
    if(arr[i].id==item.id){
      return true;
    }
  }
  return false;
}
function removeItemFromArr(arr,item,attribute){
  var index =-1;
  for (var i = 0; i < arr.length; i++) {
    if (arr[i][attribute] == item[attribute]) {
      index=i;
      break;
    }
  }
  if(index>=0){
    arr.splice(index, 1);
  }
}
module.exports = {
  formatTime: formatTime,
  formatNumber: formatNumber,
  generateOrderId: generateOrderId,
  arrayInclude: arrayInclude,
  removeItemFromArr: removeItemFromArr
}